package com.hejia.bulbul.controller;

import java.util.List;

import javax.annotation.Resource;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.hejia.alauda.entity.FinancialAsset;
import com.hejia.alauda.entity.FinancialAssetStockEntry;
import com.hejia.alauda.entity.Product;
import com.hejia.alauda.service.BalanceAndFinancingService;
import com.hejia.alauda.service.ProductService;
import com.hejia.alauda.utils.HttpResponse;

@Controller
@RequestMapping("financialassetstockentries")
public class FinancialAssetStockEntryController extends BaseController{
	
	private static final Log Logger = LogFactory.getLog(FinancialAssetStockEntryController.class);
	
	@Resource
	private BalanceAndFinancingService financialAssetService;
	
	@Resource
	private ProductService productService;
	
	@Resource
	private BalanceAndFinancingService financialAssetStockService;
	
	@RequestMapping(value="{id}", method=RequestMethod.DELETE)
	@ResponseBody
	public HttpResponse delete(@PathVariable Long id){
		
		HttpResponse response = new HttpResponse();
		
		try {
			
			this.financialAssetStockService.deleteFinancialAssetStockEntryById(id);
			response.setCode(HttpResponse.CODE_OK);
			response.setStatus(HttpResponse.STATUS_SUCCESS);
			response.setMessage("删除出入库记录成功");
			
		} catch (Exception e) {
			Logger.error("删除出入库记录失败", e);
			response.setCode(HttpResponse.CODE_INTERNAL_SERVER_ERROR);
			response.setStatus(HttpResponse.STATUS_FAIL);
			response.setMessage("删除出入库记录失败 " + e.getMessage());
		}
		
		return response;
	}
	
	@RequestMapping("add")
	public String add(
			Long financialAssetId, 
			Long productId,
			ModelMap model){
		
		FinancialAsset financialAsset = financialAssetService.findFinancialAssetById(financialAssetId);
		Product product = new Product();
		product.setId(productId);
		
		List<Product> products = productService.findProductsByFinancialAssetId(financialAssetId);
		
		FinancialAssetStockEntry financialAssetStockEntry = new FinancialAssetStockEntry();
		financialAssetStockEntry.setFinancialAsset(financialAsset);
		financialAssetStockEntry.setProduct(product);
		
		model.put("financialAsset", financialAsset);
		model.put("products", products);
		model.put("financialAssetStockEntry", financialAssetStockEntry);
		
		return "financialassetstockentry/add";
	}
	
	@RequestMapping(method = RequestMethod.POST)
	public String add(
			FinancialAssetStockEntry financialAssetStockEntry,
			ModelMap model){
		
		Long financialAssetId = financialAssetStockEntry.getFinancialAsset().getId();
		try{
			
			FinancialAsset financialAsset = financialAssetService.findFinancialAssetById(financialAssetId);
			Product product = productService.getProductById(financialAssetStockEntry.getProduct().getId());

			financialAssetStockEntry.setFinancialAsset(financialAsset);
			financialAssetStockEntry.setProduct(product);
			
			financialAssetStockService.addFinancialAssetStockEntry(financialAssetStockEntry);
			
			return String.format("redirect:/financialassets/%s/inoutstockdetail", financialAssetId);
		}catch(Exception e){
			Logger.error("添加库存记录失败", e);
			
			FinancialAsset financialAsset = financialAssetService.findFinancialAssetById(financialAssetId);
			financialAssetStockEntry.setFinancialAsset(financialAsset);
			List<Product> products = productService.findProductsByFinancialAssetId(financialAssetId);
			
			model.put("error", "添加出入库记录失败 " + e.getMessage());
			model.put("financialAsset", financialAsset);
			model.put("products", products);
			
			return "financialassetstockentry/add";
		}
	}
	
	@RequestMapping(value = "{financialAssetId}/recreate", method = RequestMethod.POST)
	@ResponseBody
	public HttpResponse recreateFinancialAssetStockEntry(@PathVariable Long financialAssetId){
		
		HttpResponse response = new HttpResponse();
		
		try {
			this.financialAssetStockService.recreateFinancialAssetStockEntry(financialAssetId);
			response.setCode(HttpResponse.CODE_OK);
			response.setStatus(HttpResponse.STATUS_SUCCESS);
			response.setMessage("重建出入库记录成功");
			
		} catch (Exception e) {
			Logger.error("重建出入库记录失败", e);
			response.setCode(HttpResponse.CODE_INTERNAL_SERVER_ERROR);
			response.setStatus(HttpResponse.STATUS_FAIL);
			response.setMessage("重建出入库记录失败 " + e.getMessage());
		}
		
		return response;
	}
}
